package com.haiheng.module_main.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.viewbinding.ViewBinding
import com.haiheng.common.constant.RourePage
import com.haiheng.common.utils.BookUtil
import com.haiheng.library_base.adapter.BaseAdapter
import com.haiheng.module_main.R
import com.haiheng.library_base.db.BookInfoBean
import com.haiheng.module_main.databinding.ItemBookListBinding

/**
 *
 * @Des:
 * @Author: hwz
 * @Date:2022/8/9 0009
 * @Version: 1.0
 */
class BookListAdapter : BaseAdapter<BookInfoBean>() {
    private var isHidenCheck = false
    private var pageType:String = ""

    fun setPageType(type:String){
        pageType = type
    }
    fun setHidenCheck(isHiden:Boolean){
        isHidenCheck = isHiden
    }
    override fun onCreateViewBinding(viewType: Int): (LayoutInflater, ViewGroup, Boolean) -> ViewBinding {
        return ItemBookListBinding::inflate
    }

    override fun onItemView(holder: ViewBindHolder, position: Int, item: BookInfoBean) {
        val binding = holder.binding as ItemBookListBinding
        var context = holder.itemView.context
        if (isHidenCheck){
            binding.cbSelect.visibility = View.GONE
        }else{
            binding.cbSelect.visibility = View.VISIBLE
        }
        //正确在架 1 错架 2  3 不在架
        var status = BookUtil.getBookShelfStatus(item.bookOnShelfStatus)
        if (item.isRetirment){
            status = "剔旧"
        }

        if (item.isAlarmed){
            status = "报警书目"
        }

        binding.tvStatus.text = "状态：$status"
        binding.tvBookSn.setText("条码：${item.barCode}")
        binding.tvBookName.setText("名称：${item.bookTitle}")
        binding.tvStatus.setText("状态：$status")
        binding.tvLocation.setText("位置：${item.fixedContainerSub?.name?.toString()}")
        binding.cbSelect.isChecked = item.isSelect

        when(pageType){
            RourePage.FIND_AND_LOCATE.name->{
                if (item.isSelect){
                    holder.itemView.setBackgroundColor(context.getColor(R.color.color_5deb7b))
                }
            }
            RourePage.ALARMED_BOOKS.name->{
                if (item.isAlarmed && item.isSelect){
                    holder.itemView.setBackgroundColor(context.getColor(R.color.color_ff7777))
                }
            }
        }

    }
}